package club.hicode.daydayup.retry;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

import javax.swing.table.TableRowSorter;
import java.sql.SQLOutput;
import java.util.concurrent.TimeUnit;

/**
 * FailTest 的 Retry 策略
 *
 * @author Liu Chunfu
 * @date 2018-04-17 下午2:16
 **/
public class FailSafeTest {

    public static void main(String[] args) {

        RetryPolicy retryPolicy = new RetryPolicy()
                .retryOn(Throwable.class)
                .retryWhen(false)
                .withDelay(1, TimeUnit.SECONDS)
                .withMaxRetries(2);

        Failsafe.with(retryPolicy).withFallback(() -> {
            Console.error("都不对啊");
            return;
        }).run(() -> {
            int i = RandomUtil.randomInt(8);
            if (i == 6) {
                System.out.println("对了...");
                //return true;
            }
            if (i % 2 == 0) {
                System.out.println("抛出异常继续");
                throw new RuntimeException("xxx");
            } else {
                System.out.println("报错 false 继续");
                //return false;
            }
        });


        //System.out.println("结果是：" + aBoolean);
    }

}
